package com.jinmdz.fmis.api.global;

import com.jinmdz.fmis.core.constant.Constants;
import com.jinmdz.fmis.core.model.HandlerInfo;
import com.jinmdz.fmis.core.wrapper.HandlerWrapper;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.TimeZone;

/**
 * 全局上下文感知
 *
 * @author LiCongLu
 * @date 2019-12-27 13:31
 */
@Component
public class GlobalContextAware implements ApplicationContextAware {

    private ApplicationContext context;
    private HashMap<String, HandlerInfo> handlers;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
        this.handlers = HandlerWrapper.getHandlers(this.context.getBeansWithAnnotation(RestController.class));
    }

    public ApplicationContext getContext() {
        return context;
    }

    public HashMap<String, HandlerInfo> getHandlers() {
        return handlers;
    }

    /**
     * 用于处理Jackson日期转码问题
     *
     * @return
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return jacksonObjectMapperBuilder ->
                jacksonObjectMapperBuilder.timeZone(TimeZone.getTimeZone(Constants.GMT8));
    }
}
